package edu.zju.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;

public class DBHelper {
	public static void main(String argvs[]) {
		initTables();
		// insertFakeData();
	}

	public static void initTables() {
		Connection connection = MysqlUtil.getConnection();
		for (int i = 0; i < 37; i++) {
			Statement statement = null;
			String sql2 = "create table sensor"
					+ i
					+ " (id int(7) PRIMARY KEY auto_increment,usbnum int,hum int,tem int,light int,time char(30));";

			try {
				statement = connection.createStatement();
				statement.executeUpdate(sql2);
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				MysqlUtil.close(connection, statement);
			}
		}
	}

	public static void insertFakeData() {
		SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
		String curtime = formater.format(new java.util.Date());

		Connection connection = MysqlUtil.getConnection();
		Statement statement = null;
		try {
			statement = connection.createStatement();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		for (int i = 0; i < 37; i++) {
			String sql2 = "INSERT INTO sensor" + i
					+ "(usbnum,hum,tem,light,time) VALUES (" + i
					+ ", 1600,6100,49,'" + curtime + "');";
			try {
				statement.executeUpdate(sql2);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		MysqlUtil.close(connection, statement);
	}
}
